package com.qut.graduate.busrtquery.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qut.graduate.busrtquery.BRTQConstant;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.bean.RouteStep;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;

import java.util.List;

/**
 * 公交路线规划详情页面的recyclerView的适配器
 */
public class BusRouteDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "BusRouteDetailAdapter";
    private Context mContext;
    private List<RouteStep> stepList;

    public BusRouteDetailAdapter(List<RouteStep> stepList, Context context) {
        this.stepList = stepList;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return BRTQConstant.BUS_ROUTE_DETAIL_FIRST; // 起点
        if (position == getItemCount() - 1) return BRTQConstant.BUS_ROUTE_DETAIL_LAST; // 终点
        if (position > 0 && position < getItemCount() - 1) {
            if (position % 2 == 1) return BRTQConstant.BUS_ROUTE_DETAIL_WALK; // 步行布局
            else return BRTQConstant.BUS_ROUTE_DETAIL_BUS; // 公交布局
        }
        return BRTQConstant.BUS_ROUTE_DETAIL_OTHER; // 其他情况
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == BRTQConstant.BUS_ROUTE_DETAIL_FIRST) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_route_detail_first, parent, false);
            return new FirstHolder(view);
        }
        if (viewType == BRTQConstant.BUS_ROUTE_DETAIL_LAST) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_route_detail_last, parent, false);
            return new LastHolder(view);
        }
        if (viewType == BRTQConstant.BUS_ROUTE_DETAIL_WALK) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_route_detail_walk, parent, false);
            return new MiddleWalkHolder(view);
        }
        if (viewType == BRTQConstant.BUS_ROUTE_DETAIL_BUS) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_route_detail_bus, parent, false);
            return new MiddleBusHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (null != mOnItemClickListener) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onClick(holder.itemView, position);
                }
            });
        }
        if (holder instanceof FirstHolder) {
            FirstHolder firstHolder = (FirstHolder) holder;
            firstHolder.routeFirstText.setText("起点");
            firstHolder.routeFirstIcon.setImageResource(R.mipmap.icon_place_start);
        }
        if (holder instanceof LastHolder) {
            LastHolder lastHolder = (LastHolder) holder;
            lastHolder.routeLastText.setText("终点");
            lastHolder.routeLastIcon.setImageResource(R.mipmap.icon_place_end);
        }
        if (holder instanceof MiddleBusHolder) {
            MiddleBusHolder middleBusHolder = (MiddleBusHolder) holder;
            String titleForBusName = stepList.get(position - 1).getNodeTitle();
            String titleForBusStationNum = stepList.get(position - 1).getNodeTitle();
            String titleForEndStation = stepList.get(position - 1).getNodeTitle();
            String titleForStartStation = stepList.get(position - 2).getNodeTitle();
            if (titleForBusName.contains("路")) {
                middleBusHolder.routeBusIcon.setImageResource(R.mipmap.icon_bus_route_daba);
            } else if (titleForBusName.contains("地铁")) {
                middleBusHolder.routeBusIcon.setImageResource(R.mipmap.icon_bus_route_gaotie);
            } else {
                middleBusHolder.routeBusIcon.setImageResource(R.mipmap.icon_bus_route_daba);
            }
            if (titleForStartStation.contains("步行")) {
                titleForStartStation = titleForStartStation.substring(titleForStartStation.indexOf("达") + 1);
            } else if (titleForStartStation.contains("同站换乘")) {
                titleForStartStation = stepList.get(position - 3).getNodeTitle();
                titleForStartStation = titleForStartStation.substring(titleForStartStation.indexOf("达") + 1);
            }
            middleBusHolder.routeBusStartStation.setText(titleForStartStation);
            if (titleForBusName.contains("乘坐") && titleForBusName.contains(",")) {
                titleForBusName = titleForBusName.substring(titleForBusName.indexOf("坐") + 1, titleForBusName.indexOf(","));
            }
            middleBusHolder.routeBusBusName.setText(titleForBusName);
            if (titleForBusStationNum.contains("经过")) {
                titleForBusStationNum = titleForBusStationNum.substring(titleForBusStationNum.indexOf("过") + 1);
                if (titleForBusStationNum.contains("站")) {
                    titleForBusStationNum = titleForBusStationNum.substring(0, titleForBusStationNum.indexOf("站") + 1);
                }
                middleBusHolder.routeBusStationNum.setText(titleForBusStationNum);
            }
            if (titleForEndStation.contains("到达")) {
                titleForEndStation = titleForEndStation.substring(titleForEndStation.indexOf("达") + 1);
                middleBusHolder.routeBusEndStation.setText(titleForEndStation);
            }
        }
        if (holder instanceof MiddleWalkHolder) {
            MiddleWalkHolder middleWalkHolder = (MiddleWalkHolder) holder;
            String title = stepList.get(position - 1).getNodeTitle();
            if (title.contains(",")) {
                title = title.substring(0, title.indexOf(","));
            }
            middleWalkHolder.routeWalkText.setText(title);
            middleWalkHolder.routeWalkIcon.setImageResource(R.mipmap.icon_walk);
        }
    }

    @Override
    public int getItemCount() {
        if (stepList != null)
            return stepList.size() == 0 ? 0 : stepList.size() + 2;
        return 0;
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener OnItemClickListener) {
        this.mOnItemClickListener = OnItemClickListener;
    }

    public class FirstHolder extends RecyclerView.ViewHolder {
        public ImageView routeFirstIcon;
        public TextView routeFirstText;

        public FirstHolder(View view) {
            super(view);
            routeFirstIcon = view.findViewById(R.id.item_route_detail_first_icon);
            routeFirstText = view.findViewById(R.id.item_route_detail_first_text);
        }
    }

    public class LastHolder extends RecyclerView.ViewHolder {
        public ImageView routeLastIcon;
        public TextView routeLastText;

        public LastHolder(View view) {
            super(view);
            routeLastIcon = view.findViewById(R.id.item_route_detail_last_icon);
            routeLastText = view.findViewById(R.id.item_route_detail_last_text);
        }
    }

    public class MiddleWalkHolder extends RecyclerView.ViewHolder {
        public ImageView routeWalkIcon;
        public TextView routeWalkText;

        public MiddleWalkHolder(View view) {
            super(view);
            routeWalkIcon = view.findViewById(R.id.item_route_detail_walk_icon);
            routeWalkText = view.findViewById(R.id.item_route_detail_walk_text);
        }
    }

    public class MiddleBusHolder extends RecyclerView.ViewHolder {
        public ImageView routeBusIcon;
        public TextView routeBusStartStation;
        public TextView routeBusBusName;
        public TextView routeBusStationNum;
        public TextView routeBusEndStation;

        public MiddleBusHolder(View view) {
            super(view);
            routeBusIcon = view.findViewById(R.id.item_route_detail_bus_icon);
            routeBusStartStation = view.findViewById(R.id.item_route_detail_bus_startStation);
            routeBusBusName = view.findViewById(R.id.item_route_detail_bus_busName);
            routeBusStationNum = view.findViewById(R.id.item_route_detail_bus_stationNum);
            routeBusEndStation = view.findViewById(R.id.item_route_detail_bus_endStation);
        }
    }


}
